package device

import (
	"encoding/json"
	"gitee.com/zhanghuamingmyself/api"
	"gitee.com/zhanghuamingmyself/util"
)

var deviceSettingBasePath = "iot/api/v1/v1/device/setting/"

type IotDeviceSettingParam struct {
	api.BaseDTO
	Name  string      `json:"name"`
	Value interface{} `json:"value"`
}

type IotDeviceSetting struct {
	api.BaseDTO
	Port      string               `json:"port"`
	KeyName   string               `json:"keyName"`
	Unit      string               `json:"unit"`
	Value     interface{}          `json:"value"`
	ValueDate int32                `json:"valueDate"`
	ParamList []IotDeviceSettingParam `json:"paramList"`
}

type IotDeviceSettingListReq struct {
	DeviceCore string `json:"deviceCore"`
	Port       string `json:"port"`
	Hidden     *bool   `json:"hidden"`
}

func ListDeviceSetting(req IotDeviceSettingListReq) ([]IotDeviceSetting, error) {
	var deviceSettingList []IotDeviceSetting
	bytes, err := util.PostJson(deviceSettingBasePath+"list", req)
	if err != nil {
		return deviceSettingList, err
	}
	json.Unmarshal(bytes, &deviceSettingList)
	return deviceSettingList, nil
}

type IotDeviceSettingRes struct {
	DeviceCore string        `json:"deviceCore"`
	MsgId      string        `json:"msgId"`
	Error      string        `json:"error"`
	Msg        string        `json:"msg"`
	Setting    IotDeviceSetting `json:"setting"`
}

type IotDeviceSettingSendReq struct {
	DeviceCore string      `json:"deviceCore"`
	KeyName    string      `json:"keyName"`
	Port       string      `json:"port"`
	OutTime    *int32       `json:"outTime"`
	Value      interface{} `json:"value"`
}

func SendDeviceSetting(req IotDeviceSettingSendReq) (IotDeviceSettingRes, error) {
	var deviceSettingRes IotDeviceSettingRes
	bytes, err := util.PostJson(deviceSettingBasePath+"send", req)
	if err != nil {
		return deviceSettingRes, err
	}
	json.Unmarshal(bytes, &deviceSettingRes)
	return deviceSettingRes, nil
}
